<template>
  <el-card shadow="always">
    <h1>{{ id ? "编辑" : "创建" }}管理员</h1>
    <el-form :v-model="form" label-width="100px">
      <el-form-item label="账号">
        <el-input v-model="form.userName" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.pwd" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save()">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      form: {
        name: "",
        parent: "",
      },
      parents: [],
    };
  },
  created() {
    this.id && this.fetch();
  },

  methods: {
    async fetch() {
      const rs = await this.$http.get(`rest/admin_users/${this.id}`);
      this.form = rs.data;
    },

    async save() {
      let rs;
      if (this.id) {
        rs = await this.$http.put(`rest/admin_users/${this.id}`, this.form);
      } else {
        rs = await this.$http.post("rest/admin_users", this.form);
      }
      if (rs.status !== 200) return;
      this.$message({
        message: "成功",
        type: "success",
      });
      this.$router.push({ path: "/admin_users/list" });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px;
}
.el-form {
  margin-top: 30px;
}
</style>
